﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HotelManageSystem.Domain;
using HotelManageSystem.Domain.Entities;
using HotelManageSystem.Domain.Infrastructure;
using HotelManageSystem.Models;

namespace HotelManageSystem.Controllers
{
    //保险系数设定
    public class InsureController : Controller
    {
        private IInsureRepository insureRepository;
        private IUnitOfWork unitOfWork;
        public InsureController(IInsureRepository insureRepository, IUnitOfWork unitOfWork)
        {
            this.insureRepository = insureRepository;
            this.unitOfWork = unitOfWork;
        }
        //HttpHet页面
        [HttpGet]
        public PartialViewResult Index()
        {
            //获取第一条数据
            Insure insure = insureRepository.GetById(1);
            if (insure == null)
            {
                //无数据,添加一条默认数据,并重新获取
                insureRepository.Add(new Insure() { Id = 1 });
                unitOfWork.Commit();
                insure = insureRepository.GetById(1);
            }

            InsureModel insureModel = new InsureModel()
                                          {
                                              EndowmentAssurance = insure.EndowmentAssurance.ToString(),
                                              MedicalInsurance = insure.MedicalInsurance.ToString(),
                                              UnemploymentInsurance = insure.UnemploymentInsurance.ToString(),
                                              EmploymentInjuryInsurance = insure.EmploymentInjuryInsurance.ToString(),
                                              MaternityInsurance = insure.MaternityInsurance.ToString(),
                                              ProvidentFund = insure.ProvidentFund.ToString()
                                          };
            return PartialView(insureModel);
        }
        [HttpPost]
        public RedirectToRouteResult Save(InsureModel insureModel)
        {
            //获取第一条数据
            Insure insure = insureRepository.GetById(1);
            if (insure == null)
            {
                //无数据,添加一条默认数据,并重新获取
                insureRepository.Add(new Insure() { Id = 1 });
                unitOfWork.Commit();
                insure = insureRepository.GetById(1);
            }

            if (ModelState.IsValid)
            {
                insure.EndowmentAssurance = Convert.ToDecimal(insureModel.EndowmentAssurance);
                insure.MedicalInsurance = Convert.ToDecimal(insureModel.MedicalInsurance);
                insure.UnemploymentInsurance = Convert.ToDecimal(insureModel.UnemploymentInsurance);
                insure.EmploymentInjuryInsurance = Convert.ToDecimal(insureModel.EmploymentInjuryInsurance);
                insure.MaternityInsurance = Convert.ToDecimal(insureModel.MaternityInsurance);
                insure.ProvidentFund = Convert.ToDecimal(insureModel.ProvidentFund);
                insureRepository.Update(insure);
                unitOfWork.Commit();
            }
            Session["url"] = "/Insure/Index";
            return RedirectToAction("Index", "HumanResources");
        }
    }
}
